<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Mongodb 3.0 配置身份验证db.createUser()说明 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Mongodb 3.0 配置身份验证db.createUser()说明</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<h2 id="定义："><a href="#定义：" class="headerlink" title="定义："></a>定义：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个数据库新用户用db.createUser()方法，如果用户存在则返回一个用户重复错误。</p>
<h2 id="语法："><a href="#语法：" class="headerlink" title="语法："></a>语法：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">db.createUser(user, writeConcern)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;user这个文档创建关于用户的身份认证和访问信息；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;writeConcern这个文档描述保证MongoDB提供写操作的成功报告。</p>
<h3 id="user文档，定义了用户的以下形式-："><a href="#user文档，定义了用户的以下形式-：" class="headerlink" title="user文档，定义了用户的以下形式**："></a>user文档，定义了用户的以下形式**：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&#123; user: <span class="string">"&lt;name&gt;"</span>,</div><div class="line">  <span class="built_in">pwd</span>: <span class="string">"&lt;cleartext password&gt;"</span>,</div><div class="line">  customData: &#123; &lt;any information&gt; &#125;,</div><div class="line">  roles: [</div><div class="line">    &#123; role: <span class="string">"&lt;role&gt;"</span>, db: <span class="string">"&lt;database&gt;"</span> &#125; | <span class="string">"&lt;role&gt;"</span>,</div><div class="line">    ...</div><div class="line">  ]</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="user文档字段介绍："><a href="#user文档字段介绍：" class="headerlink" title="user文档字段介绍："></a>user文档字段介绍：</h3><ul>
<li>user字段，为新用户的名字；</li>
<li>pwd字段，用户的密码；</li>
<li>cusomData字段，为任意内容，例如可以为用户全名介绍；</li>
<li>roles字段，指定用户的角色，可以用一个空数组给新用户设定空角色；</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在roles字段,可以指定内置角色和用户定义的角色。</p>
<h3 id="Built-In-Roles（内置角色）："><a href="#Built-In-Roles（内置角色）：" class="headerlink" title="Built-In Roles（内置角色）："></a>Built-In Roles（内置角色）：</h3><ol>
<li><p>数据库用户角色：read、readWrite;</p>
</li>
<li><p>数据库管理角色：dbAdmin、dbOwner、userAdmin；</p>
</li>
<li><p>集群管理角色：clusterAdmin、clusterManager、clusterMonitor、hostManager；</p>
</li>
<li><p>备份恢复角色：backup、restore；</p>
</li>
<li><p>所有数据库角色：readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase</p>
</li>
<li><p>超级用户角色：root  </p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>这里还有几个角色间接或直接提供了系统超级用户的访问（dbOwner 、userAdmin、userAdminAnyDatabase）</strong></p>
<ol>
<li>内部角色：__system</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>PS：关于每个角色所拥有的操作权限可以点击上面的内置角色链接查看详情</strong>。</p>
<h3 id="writeConcern文档（官方说明）"><a href="#writeConcern文档（官方说明）" class="headerlink" title="writeConcern文档（官方说明）"></a>writeConcern文档（官方说明）</h3><ul>
<li>w选项：允许的值分别是 1、0、大于1的值、”majority”、<tag set="">；</tag></li>
<li>j选项：确保mongod实例写数据到磁盘上的journal（日志），这可以确保mongd以外关闭不会丢失数据。设置true启用。</li>
<li>wtimeout：指定一个时间限制,以毫秒为单位。wtimeout只适用于w值大于1。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：在products数据库创建用户accountAdmin01，并给该用户admin数据库上clusterAdmin和readAnyDatabase的角色，products数据库上readWrite角色。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">use products</div><div class="line">db.createUser( &#123; <span class="string">"user"</span> : <span class="string">"accountAdmin01"</span>,</div><div class="line">                 <span class="string">"pwd"</span>: <span class="string">"cleartext password"</span>,</div><div class="line">                 <span class="string">"customData"</span> : &#123; employeeId: 12345 &#125;,</div><div class="line">                 <span class="string">"roles"</span> : [ &#123; role: <span class="string">"clusterAdmin"</span>, db: <span class="string">"admin"</span> &#125;,</div><div class="line">                             &#123; role: <span class="string">"readAnyDatabase"</span>, db: <span class="string">"admin"</span> &#125;,</div><div class="line">                             <span class="string">"readWrite"</span></div><div class="line">                             ] &#125;,</div><div class="line">               &#123; w: <span class="string">"majority"</span> , wtimeout: 5000 &#125; )</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;验证：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongo -u accountAdmin01 -p yourpassward --authenticationDatabase products</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/10.   Mongodb 3.0 配置身份验证db.createUser()说明/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
